﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ClassLibrary.DAO;

namespace mprsc
{
    public partial class SearchCustomer : Form
    {
        TblCustomersDAO customerDAO = new TblCustomersDAO(Global.CONNECTION_STRING);

        public SearchCustomer()
        {
            InitializeComponent();
            LoadCustomerList();
            if (grdCus.ColumnCount > 1)
            {
                btnDetailCus.Enabled = true;
            }
            else
            {
                btnDetailCus.Enabled = false;
            }

        }
        private void LoadCustomerList()
        {
            try
            {
                DataTable dt = customerDAO.GetCustomerList(txtCompany.Text,
                                             txtFirstName.Text, txtLastName.Text, txtMobilePhone.Text, "");
                grdCus.DataSource = dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            LoadCustomerList();
        }

        private void SearchCustomer_Load(object sender, EventArgs e)
        {
            this.CenterToParent();
        }

        private void btnDetailCus_Click(object sender, EventArgs e)
        {
                string idC = grdCus.CurrentRow.Cells["CustomerIndex"].Value.ToString();
                //Tạo AddNewCustomer
                NewCustomer addnewCus = new NewCustomer();
                addnewCus.Sender(idC); //Gọi delegate
                this.Hide();
                addnewCus.Show();
            
        }      

        private void button1_Click(object sender, EventArgs e)
        {
            txtCompany.Text = txtFirstName.Text = txtLastName.Text = txtMobilePhone.Text = string.Empty;
            txtFirstName.Focus();
        }

        private void btnNewCus_Click(object sender, EventArgs e)
        {
            this.Hide();
            NewCustomer newCus = new NewCustomer();
            newCus.Show();
        }
    }
}
